package com.sx.zongzhi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Spinner;

import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.PopulationFixedDetailBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/11/28.
 * 留守人员业务信息添加或编辑
 */
public class FixedBusinessEditFragment extends Fragment implements IPublicBeanView{

    @BindView(R.id.sp_Health) Spinner sp_Health;
    @BindView(R.id.et_YearInCome) EditText et_YearInCome;
    @BindView(R.id.sp_FixedType) Spinner sp_FixedType;
    @BindView(R.id.sp_Remark) Spinner sp_Remark;
    @BindView(R.id.et_IDCardNum) EditText et_IDCardNum;
    @BindView(R.id.et_Name) EditText et_Name;
    @BindView(R.id.et_Phone) EditText et_Phone;
    @BindView(R.id.et_Telephone) EditText et_Telephone;
    @BindView(R.id.sp_FaMHealth) Spinner sp_FaMHealth;
    @BindView(R.id.sp_Relation) Spinner sp_Relation;
    @BindView(R.id.et_FaMInCome) EditText et_FaMInCome;
    @BindView(R.id.et_FaMAddress) EditText et_FaMAddress;
    @BindView(R.id.et_Difficult) EditText et_Difficult;
    @BindView(R.id.et_Help) EditText et_Help;

    private PopulationFixedDetailBean detailBean;
    private boolean isEdit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fixed_infor_edit,container,false);
        ButterKnife.bind(this,view);
        detailBean= (PopulationFixedDetailBean) getArguments().getSerializable("PopulationFixedDetailBean");
        isEdit=getArguments().getBoolean("isEdit");
        showViews();
        initData();
        return view;
    }


    public boolean submit(){
        GlobalBean hBean= (GlobalBean) sp_Health.getSelectedItem();
        GlobalBean fBean= (GlobalBean) sp_FixedType.getSelectedItem();
        if (fBean==null){
            ToastUtils.showToast(getActivity(),"请选择留守人员类型");
            return false;
        }
        GlobalBean rBean= (GlobalBean) sp_Remark.getSelectedItem();
        if (rBean==null){
            ToastUtils.showToast(getActivity(),"请选择人户一致标识");
            return false;
        }
        String idCard=et_IDCardNum.getText().toString().trim();
        if (BaseUtil.isEmpty(idCard)){
            et_IDCardNum.setError("30字以内");
            ToastUtils.showToast(getActivity(),"家庭主要成员身份证号");
            return false;
        }
        String name=et_Name.getText().toString().trim();
        if (BaseUtil.isEmpty(name)){
            et_IDCardNum.setError("15字以内");
            ToastUtils.showToast(getActivity(),"家庭主要成员姓名");
            return false;
        }
        String phone=et_Phone.getText().toString().trim();
        String tel=et_Telephone.getText().toString().trim();
        if (BaseUtil.isEmpty(phone)&&BaseUtil.isEmpty(tel)){
            et_Phone.setError("至少输入一种联系方式");
            ToastUtils.showToast(getActivity(),"至少输入一种联系方式");
            return false;
        }
        GlobalBean fhBean= (GlobalBean) sp_FaMHealth.getSelectedItem();
        GlobalBean relBean= (GlobalBean) sp_Relation.getSelectedItem();
        if (relBean==null){
            ToastUtils.showToast(getActivity(),"请选择与留守人关系");
            return false;
        }
        detailBean.setHealth(hBean);
        String inCome=et_YearInCome.getText().toString().trim();
        detailBean.setPersonalAnnualIncome(BaseUtil.isEmpty(inCome)?
                0:Integer.parseInt(inCome));
        detailBean.setLeftType(fBean);
        detailBean.setHouseholdIdentity(rBean);
        detailBean.setFamilyMainIdCard(idCard);
        detailBean.setFamilyMainName(name);
        detailBean.setFamilyMainPhone(phone);
        detailBean.setFamilyTelephone(tel);
        detailBean.setFamilyMainHealth(fhBean);
        detailBean.setLeftRelationship(relBean);
        String fInCome=et_FaMInCome.getText().toString().trim();
        detailBean.setFamilyAnnualIncome(BaseUtil.isEmpty(fInCome)?
            0:Integer.parseInt(fInCome));
        detailBean.setFamilyMainAdress(et_FaMAddress.getText().toString().trim());
        detailBean.setDifficultiesAndDemands(et_Difficult.getText().toString().trim());
        detailBean.setHelpingSituation(et_Help.getText().toString().trim());
        return true;
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("健康状况");
        publicListPest.getPublicList("留守人员类型");
        publicListPest.getPublicList("人户状态");
        publicListPest.getPublicList("与户主关系");
    }

    private void showViews() {
        if (isEdit){
            et_YearInCome.setText(detailBean.getPersonalAnnualIncome()+"");
            et_IDCardNum.setText(detailBean.getFamilyMainIdCard());
            et_Name.setText(detailBean.getFamilyMainName());
            et_Phone.setText(detailBean.getMobileNumber());
            et_Telephone.setText(detailBean.getTelephone());
            et_FaMInCome.setText(detailBean.getFamilyAnnualIncome()+"");
            et_FaMAddress.setText(detailBean.getFamilyMainAdress());
            et_Difficult.setText(detailBean.getDifficultiesAndDemands());
            et_Help.setText(detailBean.getHelpingSituation());
        }
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        String id = "";
        if ("健康状况".equals(publicBean.getDomainName())) {
            if (detailBean != null && detailBean.getHealth() != null) {
                id = detailBean.getHealth().getId();
            }
            showSpinner(sp_Health, publicBean.getDicts(),id);
            if (detailBean!=null&&detailBean.getFamilyMainHealth()!=null){
                id = detailBean.getFamilyMainHealth().getId();
            }
            showSpinner(sp_FaMHealth, publicBean.getDicts(),id);
        }
        if ("留守人员类型".equals(publicBean.getDomainName())) {
            if (detailBean != null && detailBean.getLeftType() != null) {
                id = detailBean.getLeftType().getId();
            }
            showSpinner(sp_FixedType, publicBean.getDicts(),id);
        }
        if ("人户状态".equals(publicBean.getDomainName())) {
            if (detailBean != null && detailBean.getHouseholdIdentity() != null) {
                id = detailBean.getHouseholdIdentity().getId();
            }
            showSpinner(sp_Remark, publicBean.getDicts(),id);
        }
        if ("与户主关系".equals(publicBean.getDomainName())) {
            if (detailBean != null && detailBean.getLeftRelationship() != null) {
                id = detailBean.getLeftRelationship().getId();
            }
            showSpinner(sp_Relation, publicBean.getDicts(),id);
        }
    }

    private void showSpinner(Spinner spinner, List<GlobalBean> datas, String id) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(getActivity(),R.layout.item_text,datas);
        spinner.setAdapter(adapter);
        if (!BaseUtil.isEmpty(id)){
            int count=datas.size();
            for (int i=0;i<count;i++){
                if (id.equals(datas.get(i).getId())){
                    spinner.setSelection(i);
                    break;
                }
            }
        }

    }
}
